package ModelLayer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Write a description of class FileGroup here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FileGroup
{
    private File file;
    private BufferedWriter bw;
    private FileWriter fw;
    private BufferedReader br;
    private FileReader fr;
    private HashMap<String, Group> old_groups;

    /**
     * Constructor for objects of class FileGroup
     */
    public FileGroup()
    {
         file = new File("Groups.txt");
       if(!(file.exists()))
       {
           try
           {
               file.createNewFile();
           }
           
           catch (IOException e)
           {
               
           }
       }
       old_groups = get_data();
    }
    
    public boolean save_changes(HashMap <String, Group> new_groups)
    {
        if(has_changed(new_groups))
        {
            
            try
            {
                fw = new FileWriter(file);
                bw = new BufferedWriter(fw);
                Iterator<Map.Entry<String, Group>> new_it = new_groups.entrySet().iterator();       
                while (new_it.hasNext())
                {
                    Map.Entry<String, Group> new_entry = new_it.next();
                    String line = new_entry.getKey() + "\t" + new_entry.getValue().get_terms() + "\t" + new_entry.getValue().get_discount();
                    bw.write(line);
                    bw.newLine();
                }
                bw.close();
                old_groups = get_data();
                return true;
            }
            catch (IOException e)
            {
                return false;
            }
        }
        return true;
    }
    private boolean has_changed(HashMap <String, Group> new_groups)
    {
        if((old_groups.size()) == new_groups.size())
        {
            Iterator<Map.Entry<String, Group>> old_it = old_groups.entrySet().iterator();
            Iterator<Map.Entry<String, Group>> new_it = new_groups.entrySet().iterator();       
            while (old_it.hasNext())
            {
                Map.Entry<String, Group> old_entry = old_it.next();
                Map.Entry<String, Group> new_entry = new_it.next();
                if (!old_entry.getKey().equals(new_entry.getKey()))
                {
                     return true;
                }
                if (!old_entry.getValue().get_terms().equals(new_entry.getValue().get_terms()))
                {
                     return true;
                }
                
                if (old_entry.getValue().get_discount()!=new_entry.getValue().get_discount())
                {
                     return true;
                }
            }
            return false;
        }
        return true;
    }
    
    public HashMap<String, Group> get_data()
    {
        HashMap<String, Group> groups_from_file = new HashMap<>();
        String line;
        try
        {
            fr = new FileReader(file);
            try
            {
                br = new BufferedReader(fr);
                
                while ((line = br.readLine()) != null)
                {
                    if(line != "")
                    {
                        ArrayList<String> list = new ArrayList<String>(Arrays.asList(line.split("\t")));
                        groups_from_file.put(list.get(0), new Group(list.get(0), list.get(1), Double.parseDouble(list.get(2))));
                    }
                }
                br.close();
            }
            catch (IOException e)
            {
            }
        }
        catch (FileNotFoundException ex)
        {
        }
        return groups_from_file;
    }
    
}
